package validate;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 对对象进行校验.
 * 
 *
 * VERSION        NAME        DATE        REASON
 * ----------------------------------------------------------------------------
 * 1.00           Brian       2011/10/27  Create
 * @author brian
 * @version 1.00
 */
public class ValidateUtils {
    public static final String EMAIL_ADDRESS_PATTERN = "\\b(^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\\.[A-Za-z0-9-]+)*((\\.[A-Za-z0-9]{2,})|(\\.[A-Za-z0-9]{2,}\\.[A-Za-z0-9]{2,}))$)\\b";
    public static final String MOBILE_PATTERN        = "\\d{8,12}";
    public static final String LANDLIND_PATTERN      = "((0\\d{2,3})-)(\\d{7,8})(-(\\d{1,3}))?";
    public static final String NUMBER_PATTERN        = "(\\+|-){0,1}\\d+.{0,1}\\d*";

    /**
     * 检验对象是否为空,String 中只有空格在对象中不算空.<BR>
     *
     * @param object
     * @return 为空返回true,否则false.
     */
    public static boolean isEmpty(Object object) {

        if (null == object)
            return true;
        else if (object instanceof String)
            return "".equals(object.toString());
        else if (object instanceof Iterable)
            return ((Iterable<?>) object).iterator().hasNext();
        else if (object.getClass().isArray())
            return Array.getLength(object) == 0;
        else if (object instanceof Map)
            return ((Map<?, ?>) object).size() == 0;
        else if (Number.class.isAssignableFrom(object.getClass()))
            return false;
        else if (Date.class.isAssignableFrom(object.getClass()))
            return false;
        else
            return false;
    }

    /**
     * 检验字符串是否为空,空格也算字符,off 是否去首尾空格(true: 去掉首尾空格).<BR>
     *
     * @param string
     * @param off
     * @return 为空返回true,否则false.
     */
    public static boolean isEmpty(String string, boolean off) {

        if (null == string)
            return true;
        if (off)
            string = string.trim();
        return "".equals(string);
    }

    /**
     * 判断对象是否是数字.<BR>
     *
     * @return 是数字返回true, 否则false.
     */
    public static boolean isNumber(Object object) {

        if (null == object)
            return false;
        else {
           return isMatches(object.toString(), NUMBER_PATTERN);
        }
    }

    /**
     * 判断数字是否是负数数字.<BR>
     *
     * @return 是负数返回true, 否则false.
     */
    public static boolean isNegativeNumber(Object object) {

        if (null == object)
            return false;
        else {
            if (isNumber(object)) {
                String s = object.toString();
                if (s.charAt(0) == '-')
                    return true;
            }
            return false;
        }
    }

    /**
     * 判断数字是否有小数点.<BR>
     *
     * @return 是有小数点返回true, 否则false.
     */
    public static boolean isDecimalNumber(Object object) {

        String s = object.toString();
        if (s.indexOf(".") == -1) {
            return false;
        }
        return true;
    }

    /**
     * 根据format,判断字符串是否符合格式.<BR>
     *
     * @param format
     * @param date
     * @return 符合true;否则false.
     */
    public static boolean isDate(String format, String date) {

        // TODO (请完成) 不要使用exception来判断
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        try {
            dateFormat.format(dateFormat.parse(date));
            return true;
        } catch (ParseException e) {
            return false;
        }
    }

    /**
     * 是否匹配.<BR>
     *
     * @param str 字符串
     * @param regex 正则表达式
     * @param isCaseSensitive 是否忽略大小写
     * @return true匹配，false不匹配
     */
    public static boolean isMatches(String str, String regex, boolean isCaseSensitive) {

        if (isEmpty(str))
            return false;
        Pattern pattern;
        if (isCaseSensitive) {
            pattern = Pattern.compile(regex);
        } else {
            pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        }
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    /**
     * 是否匹配.<BR>
     *
     * @param str 字符串
     * @param regex 正则表达式
     * @return true匹配，false不匹配
     */
    public static boolean isMatches(String str, String regex) {

        return isMatches(str, regex, true);
    }

    /**
     * 是否email地址.<BR>
     *
     * @param email 邮箱地址
     * @return true是，false否
     */
    public static boolean isEmailAddress(String email) {

        return isMatches(email, EMAIL_ADDRESS_PATTERN, false);
    }

    /**
     * 验证固定电话..<BR>
     *
     * @param landline
     * @return 匹配true
     */
    public static boolean isLandline(String landline) {

        return isMatches(landline, LANDLIND_PATTERN);
    }

    /**
     * 验证手机号码.<BR>
     *
     * @param landline
     * @return 匹配true
     */
    public static boolean isMobile(String landline) {

        return isMatches(landline, MOBILE_PATTERN);
    }

    /**
     * 字符串最长长度 check.<BR>
     *
     * @param str
     * @param max
     * @param off 是否去首尾空格
     * @return 字符串为空时false,大于最大值时true,小于时false;
     */
    public static boolean maxLength(String str, Long max, boolean off) {

        if (off)
            str = str.trim();
        if (isEmpty(str))
            return false;
        return str.length() > max;
    }

    /**
     * 字符串最短长度check.<BR>
     *
     * @param str
     * @param max
     * @param off 是否去首尾空格
     * @return 字符串为空时true,小于最小值时true,大于时false;
     */
    public static boolean minLength(String str, Long min, boolean off) {

        if (off)
            str = str.trim();
        if (isEmpty(str))
            return true;
        return str.length() < min;
    }

    public static void main(String[] args) {

        System.out.println(isMatches("213a.421", NUMBER_PATTERN));
    }
}
